$(function(){
    WSCMobileSlide({
        "transition": "transition",
        "banner": $(".banner"),
        "slide": $(".slide"),
        "slide_img": $(".slide_img"),
        "timerNum": 3000
    });

    $(".addcart").click(function(){
        $(".widgets-cover").addClass("show");
        $(".widgets-cover .footer p").text("加入购物车");
        $("body").addClass("over");
    })

    $(".buy").click(function(){
        $(".widgets-cover").addClass("show");
        $("body").addClass("over");
        $(".widgets-cover .footer p").text("立即购买");
    })

    $(".sku-list-wrap>li span").click(function(){
        $(".sku-list-wrap span").removeClass("check");
        $(this).addClass("check");
        $(".sku-info span").text($(this).text());
    })

    $(".cover-bg").click(function(){
        $(".sku-list-wrap span").removeClass("check");
        $(".widgets-cover").removeClass("show");
        $("body").removeClass("over");
    })

    $(".price-wrap .close").click(function(){
        $(".sku-list-wrap span").removeClass("check");
        $(".widgets-cover").removeClass("show");
        $("body").removeClass("over");
    })
    
    var status = true;
    $(".list_above .item3").click(function(){
        if(status){            
            $(".list_above .item3 .footer_icon img").attr("src","images/product/icon_collection2.png");
            status=false;
        }else{
            $(".list_above .item3 .footer_icon img").attr("src","images/product/icon_collection.png");
            status=true;
        }
    })
})


// -----轮播图start-----
function WSCMobileSlide(arg_obj) {

    // 得到宽度
    function getTransform(el) {
        var style = window.getComputedStyle($(el).get(0));
        var matrix = new WebKitCSSMatrix(style.webkitTransform);
        return matrix;
    }

    //滚动
    function slideAnimate(x, y) {
        if (Math.abs(x) >= y) {
            $(".slide").css("-webkit-transform", "translate3d(0,0,0)");
        } else {
            $(".slide").css("-webkit-transform", "translate3d(" + x + "rem,0,0)");
        }
    }

    //定时
    function getSlideTimer(timeNum) {
        if(typeof(timeNum) == "undefined"){
            timeNum = 3000;
        }
        var slideTimer = setInterval(function() {
            var x = getTransform(".slide").m41;
            var width=$(document.body).width();
            x=(x/width)*10.8;
            x = x - ImgWidth;
            var i=(-x/10.8+1);
            if(i>ImgWidth_arr_length){
                i=1;
            }

            $(".slide-bt span").removeClass("on");
            $(".slide-bt span[data-slide-bt="+i+"]").addClass("on");
            slideAnimate(x, endPosition);

        }, timeNum);
        return slideTimer;
    }


    //手指滑动
    function touchDragMe($element) {
        var gundongX = 0;
        var gundongY = 0;
        var d = document;
        var g = 'getElementById';
        var moveEle = $element;
        var stx = sty = etx = ety = curX = curY = 0;
        var width=$(document.body).width();

        moveEle.on("touchstart", function(event) { //touchstart
            $element.removeClass(transitionName);
            //clearInterval(slideTimer);
            var event = event.originalEvent;
            gundongX = 0;
            gundongY = 0;


            // 元素当前位置
            etx = parseInt(getT3d(moveEle, "x"));
            ety = parseInt(getT3d(moveEle, "y"));

            // 手指位置
            stx = event.touches[0].pageX;
            sty = event.touches[0].pageY;
        });

        moveEle.on("touchmove", function(event) {
            var event = event.originalEvent;
            // 防止拖动页面
            event.preventDefault();

            // 手指位置 减去 元素当前位置 就是 要移动的距离
            gundongX = ((event.touches[0].pageX - stx)/width)*10.8;
            gundongY = event.touches[0].pageY - sty;

            // 目标位置 就是 要移动的距离 加上 元素当前位置
            curX = gundongX + etx;
            curY = gundongY + ety;
            if(curX>0){
                curX=0;
            }
            if(curX<-10.8*(ImgWidth_arr_length-1)){
                curX=-10.8*(ImgWidth_arr_length-1);
            }
            
            moveEle[0].style.webkitTransform = 'translate3d(' + curX + 'rem, ' + 0 + 'rem,0px)';


        });
        moveEle.on("touchend", function(event) {
            // 手指接触屏幕的位置
            var oriX = etx;
            var oriY = ety;

            // 手指离开屏幕的位置
            etx = curX;
            ety = curY;

            var slidePosition = 0;
            for (var i = 0; i < ImgWidth_arr_length - 1; i++) {
                if (Math.abs(etx) > ImgWidth_arr[i]) {

                    if (oriX > etx) {
                        // 左滑
                        slidePosition = -ImgWidth_arr[i + 1];
                        $(".slide-bt span").removeClass("on");
                        $(".slide-bt span[data-slide-bt="+(i+2)+"]").addClass("on");
                    } else {
                        // 右滑
                        slidePosition = -ImgWidth_arr[i];
                        $(".slide-bt span").removeClass("on");
                        $(".slide-bt span[data-slide-bt="+(i+1)+"]").addClass("on");
                    }
                }

            }
            $element.addClass(transitionName);
            slideAnimate(slidePosition, endPosition);
            //slideTimer = getSlideTimer(timerNum);
        });

        function getT3d(elem, ename) {
            var elem = elem[0];
            var str1 = elem.style.webkitTransform;
            if (str1 == "") return "0";
            str1 = str1.replace("translate3d(", "");
            str1 = str1.replace(")", "");
            var carr = str1.split(",");

            if (ename == "x") return carr[0];
            else if (ename == "y") return carr[1];
            else if (ename == "z") return carr[2];
            else return "";
        }
    }

    var transitionName = arg_obj["transition"] || "transition";
    var $banner = arg_obj["banner"] || $(".banner");
    var $slide = arg_obj["slide"] || $(".slide");
    var $slide_img = arg_obj["slide_img"] || $(".slide_img");
    var timerNum = arg_obj["timerNum"] || 3000;

    // $slide_img.width("10.8rem");
    // $slide_img.height("5.59rem");

    var ImgWidth_arr_length = $slide_img.length;
    var ImgWidth = 10.8;

    
    $slide.width((ImgWidth * ImgWidth_arr_length)+"rem");

    var SlideWidth = ImgWidth * ImgWidth_arr_length;
    // 轮播图终止位置
    var endPosition = SlideWidth - ImgWidth;


    var ImgWidth_arr = [];
    for (var i = 0; i < ImgWidth_arr_length; i++) {
        ImgWidth_arr.push(i * ImgWidth);
    }

    for(i=1;i<=ImgWidth_arr_length;i++){
        $(".slide-bt").append("<span data-slide-bt='"+i+"' ></span>");
        $(".slide-bt span").removeClass("on");
        $(".slide-bt span[data-slide-bt="+1+"]").addClass("on");
	}



    //var slideTimer = getSlideTimer(timerNum);

    touchDragMe($slide);

    //return slideTimer;
}

// -----轮播图end-----


// -----弹出层start-----


// -----弹出层end-----
